﻿using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

using Xceed.Wpf.Toolkit;
using SosaAnalysisModule.Data;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using SosaAnalysisModule.Modules;
using System.Drawing;
using System.ComponentModel;
using System.IO;

namespace SosaAnalysisModule
{
    /// <summary>
    /// Interaction logic for ImageComposer.xaml
    /// </summary>
    public partial class ImageComposer : UserControl, ISwitchable
    {
        private string searchDirectory;
        private List<RowItem> groupList;

        public ImageComposer()
        {
            InitializeComponent();
            setupListView();
        }

        #region ISwitchable Members

        public void UtilizeState(object state)
        {
            throw new NotImplementedException();
        }

        #endregion
        /// <summary>
        /// Opens choose Folder dialog
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chooseFolder(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            System.Windows.Forms.DialogResult result = dialog.ShowDialog();
            folderpathBox.Text = dialog.SelectedPath;
        }

        /// <summary>
        /// Initilized the groupList
        /// </summary>
        private void setupListView()
        {
            groupList = new List<RowItem>();
            GroupList.ItemsSource = groupList;
            groupList.Add(new RowItem("Group 1", ""));
        }
        /// <summary>
        /// Eventlistener for create image button
        /// opens file save dialog when completed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void createImage(object sender, RoutedEventArgs e)
        {
            //Check if a directory has been specified
            if (!Directory.Exists(searchDirectory))
            {
                string messageBoxText = "Do you want to select a folder?";
                string caption = "Folder Does Not Exist";
                MessageBoxButton button = MessageBoxButton.YesNo;
                MessageBoxImage icon = MessageBoxImage.Question;
                MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(messageBoxText, caption, button, icon);
                if (result == MessageBoxResult.Yes)
                    chooseFolder(null, null);
                return;
            }
            //Check if no groups are added
            if (groupList.Count < 1)
            {
                string messageBoxText = "Do you want to add a group?";
                string caption = "Not Enough Groups";
                MessageBoxButton button = MessageBoxButton.YesNo;
                MessageBoxImage icon = MessageBoxImage.Question;
                MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(messageBoxText, caption, button, icon);
                if (result == MessageBoxResult.Yes)
                    addGroup();
                return;
            }

            Group[] groupData = new Group[groupList.Count()];
            RowItem[] rowItems = groupList.ToArray();
            int length = rowItems[0].Id.Length;

            //Check that all groups have a filter string of equal length
            for (int i = 0; i < groupData.Length; i++)
            {
                String Id = rowItems[i].Id;
                if (!Id.Length.Equals(length))
                {
                    string messageBoxText = "Row " + (i + 1) + " has an incorrect filter key";
                    string caption = "Inconsistent Length of Filter Id";
                    MessageBoxButton button = MessageBoxButton.OK;
                    MessageBoxImage icon = MessageBoxImage.Hand;
                    MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(messageBoxText, caption, button, icon);
                    return;
                }

                //Check for Empty filter string
                if (Id.Equals(""))
                {
                    string messageBoxText = "Fill in all information";
                    string caption = groupList.ElementAt(i).Name + " has Empty Id";
                    MessageBoxButton button = MessageBoxButton.OK;
                    MessageBoxImage icon = MessageBoxImage.Hand;
                    MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(messageBoxText, caption, button, icon);
                    return;
                }

                groupData[i] = new Group(rowItems[i].Name, Id);
                groupData[i].color = Color.FromArgb(rowItems[i].color.R, rowItems[i].color.G, rowItems[i].color.B);
                groupData[i].data = new List<SosaData>();
            }

            ImageBuilder.CreateImage(groupData, searchDirectory);
        }
        //Eventlistener for searchDirectoy box text changed
        private void searchDirectoryChanged(object sender, TextChangedEventArgs e)
        {
            searchDirectory = folderpathBox.Text;
        }
        //Eventlistener for add group button
        private void addNewGroup(object sender, RoutedEventArgs e)
        {
            addGroup();
        }
        //Add rowitem to grouplist
        private void addGroup()
        {
            groupList.Add(new RowItem("Group " + (groupList.Count() + 1),""));
            refreshResourceCollection();
        }
        //Eventlistener for trash button
        private void removeGroup(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            RowItem row = button.DataContext as RowItem;
            groupList.Remove(row);
            refreshResourceCollection();
        }
        //Refresh the grouplist
        private void refreshResourceCollection()
        {
            ICollectionView view = CollectionViewSource.GetDefaultView(groupList);
            view.Refresh();
        }
        ////Eventlistener for back button
        private void back_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Switcher.Switch(new MainMenu());
        }

    }
}
